import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @author tbc on 2017/8/17 17:26:02.
 */
@Slf4j
public class CuratorZkTest2 {
    private static CuratorFramework client = CuratorFrameworkFactory.newClient(
            "123.57.156.196:2181",
            new ExponentialBackoffRetry(1000, 3)
    );

    private static String createNode(String path) throws Exception {
        return client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, path.getBytes());
    }

    private static boolean isExits(String path) throws Exception {
        Stat stat = client.checkExists().forPath(path);
        return stat != null;
    }

    private static List<String> getChildren(String path) throws Exception {
        return client.getChildren().forPath(path);
    }

    public static void main(String[] args) throws Exception {
        client.start();

//        String root = "/test/config/edusky-basic";
//        System.out.println(createNode(root));
//
//        boolean isExit = isExits(root);
//        log.info("isExit: {}", isExit);
        String root = "/curator/config/config-test";
        System.out.println(createNode(root));
        System.out.println(createNode(root + "/key1"));
        System.out.println(createNode(root + "/key2"));
        System.out.println(createNode(root + "/key3"));
        System.out.println(getChildren(root));


    }
}
